#
# @lc app=leetcode.cn id=21 lang=python3
#
# [21] 合并两个有序链表
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        a = ListNode()
        if l1 and l2:
            if l1.val < l2.val:
                a.val = l1.val
                a.next = self.mergeTwoLists(l1.next,l2)
            else:
                a.val = l2.val
                a.next = self.mergeTwoLists(l1,l2.next)
        elif l1:
            a.val = l1.val
            a.next = l1.next
        elif l2:
            a.val = l2.val
            a.next = l2.next
        else:
            a = None
        return a
# @lc code=end

